class JiraSync
{
    constructor(options)
    {
        this.url = options.url;
        this.token = options.token;
    }

    getUrl(uri)
    {
        return this.url + uri;
    }

    request(url, data=null, method='GET')
    {
        let headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0",
            "Content-Type":"application/json",
            "Accept": "*/*",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin"
        };
        let options = {
            "credentials": "include",
            "mode": "cors",
            "mothod": "GET"
        };

        if (method) {
            options.method = method;
        }

        if (data) {
            options.body = data;
        }

        if (this.token) {
            headers.authorization = "Bearer " + this.token;
        }

        if (data) {
            options.body = data;
        }

        options.headers = headers;

        return fetch(url, options);
    }

    getIssue(IssueKeyOrId)
    {
        return this.request(this.getUrl('/rest/api/2/issue/' + IssueKeyOrId));
    }

    pushIssueToTarget(SourceIssueKeyOrId, SourceIssueObject)
    {
        let promiseResp = this.request(this.getUrl('/rest/api/2/issue/' + SourceIssueKeyOrId), SourceIssueObject, 'POST');
        return promiseResp.then(resp => resp.json()).then(resp => {
            if (resp.statusCode!=200) {
                alert('Sync with some thing wrong!, detail see console. Sync Error:');
                console.error('#Sync Error:', resp);
            } else {
                console.debug('#Sync:', resp);
            }
            jQuery('#sync-loading').remove();
            jQuery('#sync-submit').show();
        });
    }

    syncOne(IssueKeyOrId)
    {
        let isseuPromiseResp = this.getIssue(IssueKeyOrId);
        isseuPromiseResp.then(resp => resp.json()).then(function(issueObject){
            console.debug('##Sync Issue to Target Jira', issueObject);
            let targetJira = new JiraSync(JSON.parse(localStorage.getItem('config')).target);
            // Add Mapping Data
            issueObject = targetJira.mappingFields(issueObject);
            targetJira.pushIssueToTarget(issueObject.key, JSON.stringify(issueObject)).then(function(){
                // jQuery('#sync-loading').remove();
                // jQuery('#sync-submit').show();
                // console.debug('#Sync Finish');
            });
        });
    }

    mappingFields(issueObject)
    {
        // mapping developer
        if (dataObject.getData(issueObject.key)) {
            issueObject.fields.customfield_13700 = [{
                name: dataObject.getData(issueObject.key)
            }];
        }
        console.debug('##issueObject mapping:', issueObject);
        return issueObject;
    }
}

const sourceJira = new JiraSync(JSON.parse(localStorage.getItem('config')).source);